﻿using System;
using System.Windows.Forms;
using Map_Creator.MapInfo;
using Map_Creator.XNA;
using Microsoft.Xna.Framework;

namespace Map_Creator.Painters
{
    class ObjectPainter : IPainter
    {
        public ObjectLayer Layer;

        private Point selectedObject = new Point(-1,-1);

        public ObjectPainter(ObjectLayer layer)
        {
            Layer = layer;
        }

        public Rectangle OnMouseDown(Point position, MouseButtons buttons)
        {
            if (selectedObject == position)
            {
                var editor = new ObjectEditor(Layer.Data[position.X,position.Y]);
                editor.ShowDialog();
                if (editor.DialogResult == DialogResult.OK)
                    Layer.Data[position.X, position.Y] = editor.Data;
                return new Rectangle();
            }

            var prevPos = selectedObject;
            selectedObject = position;
            return new Rectangle(Math.Min(prevPos.X, position.X),
                Math.Min(prevPos.Y, position.Y),
                Math.Max(prevPos.X, position.X) + 1,
                Math.Max(prevPos.Y, position.Y) + 1);
        }

        public Rectangle OnMouseMove(Point position, MouseButtons buttons)
        {
            return new Rectangle();
        }

        public Rectangle OnMouseLeave()
        {
            return new Rectangle();
        }

        public Rectangle OnMouseUp()
        {
            return Rectangle.Empty;
        }

        public void OnDraw(XnaGraphics xnaGraphics, Rectangle destination)
        {
            if (selectedObject == new Point(-1,-1)) return;

            if (destination.Contains(selectedObject))
            {
                var tileSize = Layer.AssociatedMap.TileSize;
                xnaGraphics.DrawRectangle(new Rectangle(selectedObject.X * tileSize, selectedObject.Y * tileSize, tileSize, tileSize),Color.Blue);
            }
        }
    }
}